﻿using System;
using Abp.AutoMapper;
using KelonProud.Mvc;
using KelonProud.TableManagement.Domain;
using KelonProud.TableManagement.Dtos.DataBase;
using KelonProud.TableManagement.Managers;
using Microsoft.AspNetCore.Mvc;

namespace KelonProud.TableManagement.Controllers
{
    [Route("table/[controller]/[action]")]
    public class DataBaseController: KelonProudControllerBase
    {
        private readonly KelonDataBaseManager _dataBaseManager;

        public DataBaseController(KelonDataBaseManager dataBaseManager)
        {
            _dataBaseManager = dataBaseManager;
        }

        public ActionResult Index()
        {

            return View("KelonProud.TableManagement.Views.DataBase.Index");
        }

        public ActionResult Create()
        {
            var model = new DataBaseInput()
            {
                Type = DataBaseType.MySql
            };
            return View("KelonProud.TableManagement.Views.DataBase.Edit", model);
        }

        public ActionResult Edit(int id)
        {
            var database = _dataBaseManager.GetById(id);

            if (database == null)
            {
                throw new Exception("该数据库已被删除！");
            }

            var model = database.MapTo<DataBaseInput>();
            return View("KelonProud.TableManagement.Views.DataBase.Edit", model);
        }
    }
}
